<?php
namespace app\admin\model\myshuju\bind;
use app\admin\model\myshuju\myconfigs\MyConFig;
use think\facade\Db;
use think\Request;

class MyPages extends MyConFig
{
    public $respons = [
        'total'=>0,
        'data'=>[]
    ];
    /**添加cid的page
     * @param Request $request
     * @param string $callback
     * @return \think\db\Query
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCidPage(Request $request,$cid=null,$callback=''){
        $where = $request->param();
        if($cid){
            $this->setCid($cid);
        }else{
            if($where['cid']??null){
                $this->setCid($where['cid']);
            }else{
                if($where['model_id']??null){
                    $this->setModelId($where['model_id']);
                }
            }
        }
        $limit = $request->param('limit',10);
        $where = $this->getWhere($request);
        $data = Db::name($this->table)
            ->where('delete_time',0);
        if($this->cid){
            $data = $data->where('cid',$this->cid);
        }
        if($where){
            $data = $data->where($where);
        }
        $data = $data->order('sort desc,create_time desc')
            ->paginate($limit)
            ->toArray();
        foreach ($data['data'] as $key=>$value){
            foreach ($this->field as $va){
                if($va['form_type']=='时间'){
                    $value[$va['field']] = date('Y-m-d',$value[$va['field']]);
                }
                if($va['form_type']=='单图片'){
                    $value[$va['field']] = getFilePath($value[$va['field']]);
                }
                if($va['form_type']=='无限联动框'){
                    $shuju =$value[$va['field']];
                    $table = Db::name(getTable('models'))
                        ->where('id',$va['table'])
                        ->find();
                    $news = Db::name($table['table'])
                        ->where('id',$shuju)
                        ->find();
                    if($va['option']){
                        $value[$va['field']] = $news[$va['option']];
                    }else{
                        $value[$va['field']] = $news['name'];
                    }
                }
                if($va['form_type']=='多图片'){
                    $value[$va['field']] = getFilePath($value[$va['field']],true);
                }
                if($va['form_type']=='联动下拉框'||$va['form_type']=='联动单选框'||$va['form_type']=='联动单行文本'){
                    if(!$value[$va['field']]){
                        $value[$va['field']] = $va['defaults'];
                        continue;
                    }
                    $table = Db::name(getTable('models'))
                        ->where('id',$va['table'])
                        ->find();
                    $shu = Db::name($table['table'])
                        ->where('id',$value[$va['field']])
                        ->find();
                    if($va['option']){
                        $value[$va['field']] = $shu[$va['option']];
                    }else{
                        if($shu['name']??null){
                            $value[$va['field']] = $shu['name'];
                        }elseif ($shu['phone']??null){
                            $value[$va['field']] = $shu['phone']??$shu['user'];
                        }else{
                            $value[$va['field']] = '没有设置';
                        }
                    }
//                    $value[$va['field']] = $shu['name'];
                }
                if($va['form_type']=='联动多选框'){
                    $table = Db::name(getTable('models'))
                        ->where('id',$va['table'])
                        ->find();
                    $shu = Db::name($table['table'])
                        ->where('id','in',$value[$va['field']])
                        ->select()->toArray();
                    $str = '';
                    foreach ($shu as $vs){
                        if($va['option']){
                            $str.=','.$vs[$va['option']];
                        }else{
                            if($vs['name']??null){
                                $str.=','.$vs['name'];
                            }elseif ($vs['user']??null){
                                $str.=','.$vs['user'];
                            }elseif ($vs['phone']??null){
                                $str.=','.$vs['phone'];
                            }else{
                                $str.=',未设置';
                            }
                        }
                    }
                    $str = trim($str,',');
                    $value[$va['field']] = $str;
                }
                if($va['form_type']=='联动下拉组'){
                    $table1 = explode(',',$va['table']);
                    $table1 = Db::name(getTable('models'))->where('id',$table1[1])->find();
                    $mydata = Db::name($table1['table'])->where('id',$value[$va['field']])->where('status',1)->field('name')->find();
                    if($mydata){
                        $value[$va['field']] = $mydata['name'];
                    }else{
                        $value[$va['field']] = '';
                    }
                }
                if($va['form_type']=='json数组'){
                    $str = '';
                    if($value[$va['field']]){
                        $newdatas = json_decode($value[$va['field']],true);
                        foreach ($newdatas as $ks=>$vs){
                            $str = $str.$ks.'=>'.$vs.'|';
                        }
                        $str = trim($str,'|');
                    }
                    $value[$va['field']] = $str;
                }
                if($va['form_type']=='单选框'){
                    $option = explode(',',$va['option']);
                    $option = array_flip($option);
                    $keey = $option[$value[$va['field']]];
                    $option = [
                        '',
                        'green',
                        'red',
                        'orange',
                        'young',
                        'purple',
                    ];
                    if($option[$keey]){
                        $keey = $option[$keey];
                    }else{
                        $keey = '';
                    }
                    $value[$va['field']] = '<span style="color: '.$keey.'">'.$value[$va['field']].'</span>';
                }
            }
            if($callback){
                $value = $callback($value);
            }
            $data['data'][$key] = $value;
        }
        return $data;
    }

    /**获取联表数据
     * @param Request $request
     * @param string $callback
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getModelPage(Request $request,$model_id=null,$callback='',$order='create_time desc'){
        if($model_id){
            $this->setModelId($model_id);
        }else{
            if($request->param('model_id')){
                $this->setModelId($request->param('model_id'));
            }
        }
        $where = $this->getWhere($request);
//        echo '<pre>';
//        print_r(request()->controller());
//        exit;
        $newwhere = [
            'model_id',
            '=',
            2
        ];
//        print_r($where==$newwhere);
//        exit;
        if(\request()->controller()=='Mycolumn'){
            $where = [];
        }
//        $where = [];
        $data = $this->getModelList(0,0,$where,$order)->respons;
//        echo '<pre>';
//        print_r($data);
//        exit;
        $respos['total'] = $data['total'];
        $data = $data['data'];
        foreach ($data as $key=>$value){
            foreach ($this->field as $va){
                if($va['form_type']=='时间'){
                    $value[$va['field']] = date('Y-m-d',$value[$va['field']]);
                }
                if($va['form_type']=='单图片'){
                    $value[$va['field']] = getFilePath($value[$va['field']]);
                }
                if($va['form_type']=='多图片'){
                    $value[$va['field']] = getFilePath($value[$va['field']],true);
                }
                if($va['field']=='pid'){
                    continue;
                }
                if($va['form_type']=='联动下拉框'||$va['form_type']=='联动单选框'||$va['form_type']=='联动单行文本'){
                    if(!$value[$va['field']]){
                        $value[$va['field']] = '最上级';
                        continue;
                    }
                    $table = Db::name(getTable('models'))
                        ->where('id',$va['table'])
                        ->find();
                    $shu = Db::name($table['table'])
                        ->where('id',$value[$va['field']])
                        ->find();
                    $op = 'name';
                    if(!empty($va['option'])){
                        $op = $va['option'];
                    }
                    $value[$va['field']] = $shu[$op];
                }
                if($va['form_type']=='无限联动框'){
                    $shuju =$value[$va['field']];
                    $table = Db::name(getTable('models'))
                        ->where('id',$va['table'])
                        ->find();
                    $news = Db::name($table['table'])
                        ->where('id',$shuju)
                        ->find();
                    $value[$va['field']] = $news['name'];
                }
                if($va['form_type']=='联动下拉组'){
                    $table1 = explode(',',$va['table']);
                    $table1 = Db::name(getTable('models'))->where('id',$table1[1])->find();
                    $mydata = Db::name($table1['table'])->where('id',$value[$va['field']])->where('status',1)->field('name')->find();
                    if($mydata){
                        $value[$va['field']] = $mydata['name'];
                    }else{
                        $value[$va['field']] = '';
                    }
                }
                if($va['form_type']=='json数组'){
                    $str = '';
                    if($value[$va['field']]){
                        $newdatas = json_decode($value[$va['field']],true);
                        foreach ($newdatas as $ks=>$vs){
                            $str = $str.$ks.'=>'.$vs.'|';
                        }
                        $str = trim($str,'|');
                    }
                    $value[$va['field']] = $str;
                }
            }
            if($callback){
                $value = $callback($value);
            }
            $data[$key] = $value;
        }
        $respos['data'] = $data;
        return $respos;

    }

    /**获取联表数据
     * @param int $pid
     * @param int $chengji
     * @param $where
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getModelList($pid=0,$chengji=0,$where,$order='create_time desc'){
        if($pid==0){
            $datas = Db::name($this->table)
                ->where('pid',$pid)
                ->where($where)
                ->where('delete_time',0)
                ->order('sort desc,'.$order)
                ->paginate(\request()->param('limit',10))->toArray();
            $this->respons['total'] = $datas['total'];
            $data = $datas['data'];
        }else{
            $data = Db::name($this->table)
                ->where('pid',$pid)
                ->where($where)
                ->where('delete_time',0)
                ->order('sort desc,'.$order)
                ->select()->toArray();
        }
//        echo '<pre>';
//        print_r($where);
//        exit;
//        $datas = Db::name($this->table)
//            ->where('pid',$pid)
//            ->where($where)
//            ->where('delete_time',0)
//            ->order('sort desc,'.$order)
//            ->paginate(\request()->param('limit',10))->toArray();
//        $this->respons['total'] = $datas['total'];
//        $data = $datas['data'];
//        echo '<pre>';
//        print_r($data);
//        exit;
        foreach ($data as $value){
            $value['chengji'] = $chengji;
            $value['child'] = Db::name($this->table)->where('pid',$value['id'])
                ->where($where)->where('delete_time',0)->count();
            $this->respons['data'][] = $value;
            $this->getModelList($value['id'],$chengji+1,$where);
        }
        return $this;
    }
    public static function create(){
        $obj = new MyPages();
        return $obj;
    }
}